using System.Collections.Generic;

namespace Beetle;

public class ByteArrayPool
{
	private Stack<ByteArraySegment> m_a = new Stack<ByteArraySegment>();

	private int b;

	public int Count => this.m_a.Count;

	public ByteArrayPool(int initcount, int length)
	{
		b = length;
		for (int i = 0; i < initcount; i++)
		{
			this.m_a.Push(a());
		}
	}

	private ByteArraySegment a()
	{
		return new ByteArraySegment(b);
	}

	public ByteArraySegment Pop()
	{
		lock (this)
		{
			if (this.m_a.Count > 0)
			{
				return this.m_a.Pop();
			}
			return a();
		}
	}

	public void Push(ByteArraySegment item)
	{
		lock (this)
		{
			item.SetInfo(0, 0);
			item.SetPostion(0);
			this.m_a.Push(item);
		}
	}
}
